package leetcode;

public class IsMatch {
    public static void main(String[] args) {
        boolean match = isMatch("aaa", "a*");
        System.out.println(match);
    }
    public static boolean isMatch(String s, String p) {
        int m = s.length() +1;
        int n = p.length() +1;
        boolean[][] dp = new boolean[m][n];
        dp[0][0] = true;
        for (int i = 0; i < m; i++) {
            for (int j = 1 ; j< n; j++){
                if (p.charAt(j-1) == '*'){
                    dp[i][j] = dp[i][j-2];
                    if (matches(s,p,i,j-1)){
                        dp[i][j] =dp[i][j] || dp[i-1][j];
                    }
                }else {
                    if (matches(s,p,i,j)){
                        dp[i][j] = dp[i-1][j-1];
                    }
                }
                //其余情况不处理 默认false
            }
        }
        return dp[m-1][n-1];
    }
    public static boolean matches(String s, String p, int i, int j) {
        if (i == 0) {
            return false;
        }
        if (p.charAt(j - 1) == '.') {
            return true;
        }
        return s.charAt(i - 1) == p.charAt(j - 1);
    }

}
